-- This code is likely to become convoluted so it's going in another file.
-- Porting over the cool mod icons to eval itLf (in this Lua file)

-- also this code is out of date now, but must be kept since it can deal with
-- vertical placement too. That and it works, plus I'm also lazy.

-- IF THERE'S NO PLAYER I'M GONNA PUNCH YOU!!!!!!!!!!!!!!!!!!!!!!!
local Player = ...
assert(Player);

local icons = Def.ActorFrame{};

-- it's really a pain to modify the original because I don't understand it
-- now that I go back to it. Let's try to simplify.
local function OptionIcons()
	-- make the ActorFrame to store the icons.
	local u = Def.ActorFrame{
		Name="OptionIcons"..Player;
		BeginCommand=function(self)
			-- visibility
			if not GAMESTATE:IsPlayerEnabled(Player) then
				self:visible(false);
			end;
		end;
	};

	-- I moved it here for no good reason other than I could
	local function MakeIcon(text,myX,myY)
		u[#u+1] = Def.ActorFrame{
			InitCommand=cmd(x,myX;y,myY);

			LoadActor("_optionicon")..{
				InitCommand=cmd(diffuse,PlayerColor(Player);diffusealpha,0.8);
			};
			AutoText("frutiger")..{
				Text=text;
				InitCommand=cmd(zoom,0.5;diffuse,color("#FFFFFF");NoStroke;shadowlength,1;maxwidth,90);
			};
		};
	end;

	-- set the max icons per column. (22*16 = 352 which is slightly less than
	-- SCREEN_HEIGHT for pretty much everyone.)
	-- Perhaps change this to be based on 75% of SCREEN_HEIGHT later?
	local maxIconsPerColumn = 22;

	-- get the options.
	local PlayerState = GAMESTATE:GetPlayerState(Player);
	-- grab the song options from this PlayerState.
	local options = PlayerState:GetPlayerOptionsString('ModsLevel_Preferred');
	-- now using split, let's put them into a table for comparison
	local listOfOptions = split(", ", options);
	-- find out how many options are in play.
	local numOptions = #listOfOptions;

	-- perform some checks here.
	if #listOfOptions == 0 then
		-- no options, we don't need to be here wasting my cycles which
		-- can be used up by other advanced functions.
		return;
	-- here's where it starts getting tricky.
	elseif #listOfOptions > maxIconsPerColumn then
		-- oh crap they used SO MANY MODS that we need another column.
		for i=1,#listOfOptions do
			-- set text
			local text = listOfOptions[i];

			-- find out where we are.
			-- first, horizontal.
			local xPos = 0;
			if i < (maxIconsPerColumn/2)+1 then
				-- shift first column left
				xPos = -47;
			end;
			-- then, vertical.
			local yPos = 0;
			if i > (maxIconsPerColumn/2) then
				-- reset y for second column
				yPos = 0;
			else
				yPos = i;
			end;

			MakeIcon(text,xPos,(yPos * 15)-1);
		end;
	else
		-- the more sensible version.
		for i=1,#listOfOptions do
			-- set text
			local text = listOfOptions[i];

			-- find out where we are.
			-- vertical only, there's one column!
			local yPos = 0;
			if i > (maxIconsPerColumn/2) then
				-- reset y for second column
				yPos = 0;
			else
				yPos = i;
			end;

			MakeIcon(text,0,(yPos * 15)-1);
		end;
	end;

	return u;
end;

icons[#icons+1] = OptionIcons();

return icons;